/**
 * Copyright (C) 2011-2015 The XDocReport Team <xdocreport@googlegroups.com>
 *
 * All rights reserved.
 *
 * Permission is hereby granted, free  of charge, to any person obtaining
 * a  copy  of this  software  and  associated  documentation files  (the
 * "Software"), to  deal in  the Software without  restriction, including
 * without limitation  the rights to  use, copy, modify,  merge, publish,
 * distribute,  sublicense, and/or sell  copies of  the Software,  and to
 * permit persons to whom the Software  is furnished to do so, subject to
 * the following conditions:
 *
 * The  above  copyright  notice  and  this permission  notice  shall  be
 * included in all copies or substantial portions of the Software.
 *
 * THE  SOFTWARE IS  PROVIDED  "AS  IS", WITHOUT  WARRANTY  OF ANY  KIND,
 * EXPRESS OR  IMPLIED, INCLUDING  BUT NOT LIMITED  TO THE  WARRANTIES OF
 * MERCHANTABILITY,    FITNESS    FOR    A   PARTICULAR    PURPOSE    AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE,  ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 */
package org.apache.poi.xwpf.converter.pdf;

import java.awt.*;
import java.io.*;

import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Font;
import com.itextpdf.text.pdf.BaseFont;
import css.sword.util.Doc2Docx;
import fr.opensagres.xdocreport.converter.ConverterTypeTo;
import fr.opensagres.xdocreport.converter.Options;
import fr.opensagres.xdocreport.core.XDocReportException;
import fr.opensagres.xdocreport.document.IXDocReport;
import fr.opensagres.xdocreport.document.docx.DocxConstants;
import fr.opensagres.xdocreport.document.docx.preprocessor.dom.DOMFontsPreprocessor;
import fr.opensagres.xdocreport.document.registry.XDocReportRegistry;
import fr.opensagres.xdocreport.itext.extension.font.IFontProvider;
import fr.opensagres.xdocreport.itext.extension.font.ITextFontRegistry;
import fr.opensagres.xdocreport.template.IContext;
import fr.opensagres.xdocreport.template.TemplateEngineKind;
import org.apache.poi.xwpf.converter.core.AbstractXWPFPOIConverterTest;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.junit.Test;

import fr.opensagres.poi.xwpf.converter.core.XWPFConverterException;
import fr.opensagres.poi.xwpf.converter.pdf.PdfConverter;
import fr.opensagres.poi.xwpf.converter.pdf.PdfOptions;

import static css.sword.util.Doc2Docx.doc2docx;
import static css.sword.util.PdfUtils.waterMarkerText;

public class PdfConvertChineseTestCase
{

    //@Test
    public void TestFreemarker() throws IOException, XDocReportException, DocumentException {

        // 2) Load ODS file by filling Velocity template engine and cache it
        // to the registry
        IXDocReport report = XDocReportRegistry.getRegistry().loadReport(
                new FileInputStream("C:\\Users\\12555\\Desktop\\论文草稿-新版.docx"),
                TemplateEngineKind.Freemarker);

        //8月18会议1.docx
        //智能政务办公系统2.0端口开放申请.
        //TestChineseCharacters.docx
        // 3) Create context Java model
        IContext context = report.createContext();
        context.put("comments", "world");
//        FieldsMetadata metadata = new FieldsMetadata();
//        metadata.setBeforeRowToken("@bf");
//        metadata.setAfterRowToken("@af");
//
//        report.setFieldsMetadata(metadata);
        // 4) Merge Java model with the ODS
        File file = new File("C:\\Users\\12555\\Desktop\\会议通知.pdf");


        Options options = Options.getTo(ConverterTypeTo.PDF);
        //report.process(context, out);
        //    Options options = Options.getFrom(DocumentKind.ODT).to(ConverterTypeTo.PDF);

        //   IConverter converter = ConverterRegistry.getRegistry().getConverter(options);

        report.convert(context, options, new FileOutputStream(file));
        //report.process(context, new FileOutputStream(file));
      //  report.convert(context, options, new FileOutputStream(file));
       // waterMarkerText("C:\\Users\\12555\\Desktop\\会议通知.pdf", "C:\\Users\\12555\\Desktop\\会议通知1.pdf", "我是黑豆");

    }

    @Test
    public void TestChineseCharacters()
        throws XWPFConverterException, IOException
    {
        long startTime=System.currentTimeMillis();
      //  String docFile = "C:\\Users\\12555\\Desktop\\hytz_市政府11届10次常务会议-LJW.doc";
        String docxFile = "C:\\Users\\12555\\Desktop\\套红测试.docx";
      //  doc2docx(docFile, docxFile);

        OutputStream out = new FileOutputStream(new File("C:\\Users\\12555\\Desktop\\hytz_市政府11届10次常务会议-LJW11111111.pdf"));

       // Doc2Docx.convertDocToPdf(new FileInputStream(docxFile),out);
            XWPFDocument document =
                    new XWPFDocument(new FileInputStream(docxFile));

            PdfOptions options = PdfOptions.create();

            // Customize Font provider for Chinese characters
            // This code is not optimized (font are not cached and it works only for windows)
            // Chinese people could you explain us how docx manage chinese characters?
//        options.fontProvider( new IFontProvider()
//        {
//
//            public Font getFont(String familyName, String encoding, float size, int style, Color color )
//            {
//                try
//                {
//                    BaseFont bfChinese =
//                        BaseFont.createFont( "c:/Windows/Fonts/arialuni.ttf", BaseFont.IDENTITY_H, BaseFont.EMBEDDED );
//                    Font fontChinese = new Font( bfChinese, size, style, color );
//                    if ( familyName != null )
//                        fontChinese.setFamily( familyName );
//                    return fontChinese;
//                }
//                catch ( Throwable e )
//                {
//                    e.printStackTrace();
//                    // An error occurs, use the default font provider.
//                    return ITextFontRegistry.getRegistry().getFont( familyName, encoding, size, style, color );
//                }
//            }
//        } );
            PdfConverter.getInstance().convert(document, out, options);
            out.close();

        System.out.println(System.currentTimeMillis()-startTime+"ms");
    }

//    @Test
//    public void testFonts()
//            throws IOException, XDocReportException
//    {
//        // 1) Load Docx file by filling Freemarker template engine and cache
//        // it to the registry
//        InputStream in = DocxFontsWithFreemarkerTestCase.class.getResourceAsStream( "DocxFontsWithFreemarker.docx" );
//        IXDocReport report = XDocReportRegistry.getRegistry().loadReport( in, TemplateEngineKind.Freemarker );
//
//        report.addPreprocessor( DocxConstants.WORD_DOCUMENT_XML_ENTRY, DOMFontsPreprocessor.INSTANCE );
//        report.addPreprocessor( DocxConstants.WORD_HEADER_XML_ENTRY, DOMFontsPreprocessor.INSTANCE );
//        report.addPreprocessor( DocxConstants.WORD_FOOTER_XML_ENTRY, DOMFontsPreprocessor.INSTANCE );
//
//        // 2) Create context Java model
//        IContext context = report.createContext();
//        context.put( "name", "word" );
//
//        // Change every font name+size with Magneto + 36
//        context.put( DOMFontsPreprocessor.FONT_NAME_KEY, "Magneto" );
//        context.put( DOMFontsPreprocessor.FONT_SIZE_KEY, 36 );
//
//        // 3) Generate report by merging Java model with the Docx
//        File out = new File( "target" );
//        out.mkdirs();
//        report.process( context, new FileOutputStream( new File( out, "DocxFontsWithFreemarker_Out.docx" ) ) );
//
//    }
}
